//
//  SPageBaseController.m
//  EasyRetail
//
//  Created by Samuel on 16/1/8.
//
//

#import "SPageBaseController.h"

@interface SPageBaseController ()<UIPageViewControllerDelegate,UIPageViewControllerDataSource>

@end

@implementation SPageBaseController
{
    NSInteger saveNumber;
}
+ (SPageBaseController*)shareSPageBaseController:(UIViewController *)controller
                              andFirstController:(UIViewController *)firstController
                                        andDatas:(NSMutableArray<SPageModel*>*)array;
{
    if (array.count<1) {
        return nil;
    }
    // 设置UIPageViewController的配置项
    NSDictionary *options =[NSDictionary dictionaryWithObject:[NSNumber numberWithInteger:UIPageViewControllerSpineLocationMin] forKey: UIPageViewControllerOptionSpineLocationKey];
    
    // 实例化UIPageViewController对象，根据给定的属性
    SPageBaseController *pageController = [[SPageBaseController alloc] initWithTransitionStyle:UIPageViewControllerTransitionStyleScroll navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options: options];
    pageController.dataSource = pageController;
    pageController.delegate = pageController;
    [[pageController view] setFrame:[[controller view] bounds]];
    pageController.dataArray = array;
    pageController.nowControllerViewData = array[0];
    pageController.nowController = firstController;
    // 让UIPageViewController对象，显示相应的页数据。
    // UIPageViewController对象要显示的页数据封装成为一个NSArray。
    // 因为我们定义UIPageViewController对象显示样式为显示一页（options参数指定）。
    // 如果要显示2页，NSArray中，应该有2个相应页数据。
    NSArray *viewControllers =[NSArray arrayWithObject:firstController];
    [pageController setViewControllers:viewControllers
                              direction:UIPageViewControllerNavigationDirectionForward
                               animated:YES
                             completion:nil];
    [[controller view] addSubview:[pageController view]];
    [controller addChildViewController:pageController];
    return pageController;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}


#pragma mark- UIPageViewControllerDataSource
// 返回上一个ViewController对象
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController{
//    CommunityViewController *getController = (CommunityViewController*)viewController;
    NSUInteger index = [self.dataArray indexOfObject:[viewController valueForKey:@"viewData"]];
    
    if ((index == 0) || (index == NSNotFound)) {
        //第一个页面再往前滑就木有了
        return nil;
    }
    index--;
    if (self.sDelegate && [self.sDelegate respondsToSelector:@selector(SPageBaseController:viewForRowAtIndex:)]) {
        return [self.sDelegate SPageBaseController:self viewForRowAtIndex:index];
    }
    return nil;
    
}
// 返回下一个ViewController对象
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
//    CommunityViewController *getController = (CommunityViewController*)viewController;
    NSUInteger index = [self.dataArray indexOfObject:[viewController valueForKey:@"viewData"]];
    
    if (index == NSNotFound || index == [self.dataArray count]-1) {
        return nil;
    }
    index++;
    if (self.sDelegate && [self.sDelegate respondsToSelector:@selector(SPageBaseController:viewForRowAtIndex:)]) {
        return [self.sDelegate SPageBaseController:self viewForRowAtIndex:index];
    }
    return nil;
}


- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed;
{
    UIViewController *getController = (UIViewController*)[pageViewController.viewControllers objectAtIndex:0];
    NSUInteger index = [self.dataArray indexOfObject:[getController valueForKey:@"viewData"]];
    if (index == NSNotFound) {
        return;
    }
    saveNumber = index;
    self.nowControllerViewData = self.dataArray[saveNumber];
    self.nowController = getController;
    if (self.sDelegate && [self.sDelegate respondsToSelector:@selector(SPageBaseController:viewForRowAtIndex:)]) {
        return [self.sDelegate SPageBaseController:self didScrollRowAtIndex:index andObject:self.dataArray[index]];
    }
    
}



/**
 *  重载数据
 */
- (void)reloadData;
{
    [self setThePageToIndex:saveNumber];
}
/**
 *  跳转到某个view
 *
 *  @param index
 */
- (void)setThePageToIndex:(NSInteger)index;
{
    if (self.sDelegate && [self.sDelegate respondsToSelector:@selector(SPageBaseController:viewForRowAtIndex:)]) {
        UIViewController *controller = [self.sDelegate SPageBaseController:self viewForRowAtIndex:index];
        UIPageViewControllerNavigationDirection direction = (index > saveNumber) ? UIPageViewControllerNavigationDirectionForward : UIPageViewControllerNavigationDirectionReverse;
        saveNumber = index;
        [self setViewControllers:@[controller] direction: direction animated:YES completion:nil];
        self.nowControllerViewData = self.dataArray[saveNumber];
        self.nowController = controller;
        if ([self.sDelegate respondsToSelector:@selector(SPageBaseController:didScrollRowAtIndex:andObject:)]) {
            [self.sDelegate SPageBaseController:self didScrollRowAtIndex:saveNumber andObject:self.dataArray[index]];
        }
    }
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
@implementation SPageModel

-(instancetype)initWithSPageModel:(SPageModel*)model;
{
    self = [super init];
    if (self) {
        self.sTag = model.sTag;
        self.object = model.object;
    }
    return self;
}
@end